/*
        Copyright (C) 2007  Aaron Defazio

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Net;
using System.Diagnostics;
using PluginInterface;

namespace GoogleSearchPlugin
{
    public class GoogleSearch : IPlugin
    {

        #region plugin Members

        public string Name 
        {
            get { return "Google Search Plugin"; }
        }
        public string Shortcut 
        {
            get { return "g"; }
        }

        public void ReIndex() 
        {  
        }

        private string cleanse( string term)
        {
            //TODO: Do query cleansing here
            term = term.Trim();
            term = term.Replace(' ', '+');
            return term;
        }

        public List<RObject> DoQuery(string query)
        {

            query = cleanse(query);
            /////////////////////////

            //initilize streams, http and xml stuff
            XmlSerializer serializer = new XmlSerializer(typeof(toplevel));
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://google.com/complete/search?output=toolbar&q=" + query);
            req.ContentType = "text/xml";
            req.UserAgent = "AppLn";
            //Get xml document
            toplevel title;
            using (Stream respstream = req.GetResponse().GetResponseStream())
            {
                //parse into custom autogenerated google.cs class
                title = (toplevel)parseXML(respstream, typeof(toplevel));
            }

            List<RObject>  rlist = new List<RObject>();
            rlist.Add(new RObject("", "", gicon));
            if (title.CompleteSuggestion != null)
            {
                foreach (result cr in title.CompleteSuggestion)
                {
                    rlist.Add(new RObject(cr.suggestion.data, cr.suggestion.data, gicon));
                }
            }

            if (rlist.Count <= 10)
                return rlist;
            else
                return rlist.GetRange(0, 10); ;
        }

        public void Open(string key, string QueryTerm)
        {
            string st;
            if (key == "")
                st = cleanse(QueryTerm);
            else
                st = cleanse(key);

            Process.Start("http://www.google.com/search?q=" + st);
        }

        public void Initilise()
        {
        }

        public void close() { }

        #endregion

        System.Drawing.Icon gicon;

        public GoogleSearch() {
            //Load Google Icon
            gicon = new System.Drawing.Icon("Google.ico");
            Initilise(); 
        }

        /// <summary>
        /// Taken from http://www.prescod.net/rest/dotnet/
        /// </summary>
        /// <param name="str">Use a System.Web.HttpContext   .Request.InputStream</param>
        /// <param name="type">type of object(probably autogenerated earlier) to return</param>
        /// <returns> object of specificed type(use a cast)</returns>
        object parseXML(Stream str, Type type)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            XmlReader reader = new XmlTextReader(str);
            return serializer.Deserialize(reader);
        }



    }
}
